package sadi.client;

import sadi.util.*;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;

public class WikiClient
{
        //Instance Variables
        private GUI gui;
        private WikiServer server;
        
        public WikiClient(GUI gui)
        {
                this.gui = gui;
                
                try
                {
                        server = (WikiServer) Naming.lookup("rmi://localhost:1099/WikiServer");
                }
                catch(Exception e)
                {
                        //e.printStackTrace();
                }
        }
        
        /*************** USER HANDLING METHODS ***************/
        
        //Get Introduction Message
        public String getIntroduction()
        {
                try
                {
                       return server.getIntroduction();
                }
                catch(RemoteException e)
                {
                       return ""; //e.printStackTrace();
                }
                
        }
        
        //Set Introduction Message
        public void setIntroduction(String message, int userID) 
        {
                try
                {
                        server.setIntroduction(message, userID);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                }
        }
        
        
        
        /*************** USER HANDLING METHODS ***************/
        
        //Login A User
        //Success: Return UserID
        //Failure: Return -1
        public int loginUser(String username, String password) 
        {
        	int login = -1;
                try
                {
                        login = server.loginUser(username, password);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                }
                return login;
        }
        
        //Register A User
        //Username Must Be Unique
        public boolean registerUser(String username, String password) 
        {
                try
                {
                        return server.registerUser(username, password);
                }
                catch(RemoteException e)
                {
                        return false;//e.printStackTrace();
                }
                
        }
        
        //Check User's Administrative Rights
        public boolean isAdmin(int userID)
        {
        	boolean bool = false;
                try
                {
                        bool = server.isAdmin(userID);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                }
                return bool;
        }
        
        
        
        /*************** WIKI LISTING HANDLING METHODS ***************/
        
        //Search Wiki Matches
        public ArrayList searchWiki(String title)
        {
        	ArrayList arraylist = null;
                try
                {
                	arraylist = server.searchWiki(title);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                	return new ArrayList<String>();
                }
                return arraylist;
                
        }
        
        //Get Latest Wiki Highlights
        public ArrayList<ArrayList<String>> getHighlightList() 
        {
        	
                try
                {
                        return server.getHighlightList();
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                		return new ArrayList<ArrayList<String>>();
                }
        }
        
        //Get List of Wiki with the Specified Status
        public ArrayList<ArrayList<String>> getApprovalList(int userID) 
        {
                try
                {
                       return server.getApprovalList(userID);
                }
                catch(RemoteException e)
                {
                        return new ArrayList<ArrayList<String>>();//e.printStackTrace();
                }
                
        }
        
        //Get List of Wiki with the Specified Status
        public ArrayList<ArrayList<String>> getReportedList(int userID) 
        {
                try
                {
                        return server.getReportedList(userID);
                }
                catch(RemoteException e)
                {
                	return new ArrayList<ArrayList<String>>();//e.printStackTrace();
                }               
        }
        
        
        
        /*************** WIKI HANDLING METHODS ***************/
        
      //View Content of Wiki
        public ArrayList<String> viewWiki(int wikiID, int userID) 
        {
                try
                {
                        return server.viewWiki(wikiID, userID);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                		return new ArrayList<String>();
                }

        }
        //Create a New Wiki With Content
        public void addWiki(String title, String description, String content, int userID) 
        {
                try
                {
                        server.addWiki(title, description, content, userID);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                }               
        }
        
        //Edit Wiki Contents
        public void editWiki(int wikiID, String title, String description, String content, int userID)
        {
                try
                {
                        server.editWiki(wikiID, title, description, content, userID);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                }
        }
        
        //Delete Wiki
        public void deleteWiki(int wikiID, int userID) 
        {
                try
                {
                        server.deleteWiki(wikiID, userID);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                }
        }
        
        //Report Wiki Contents
        public boolean addReport(int wikiID, int userID)
        {
                try
                {
                       return server.addReport(wikiID, userID);
                }
                catch(RemoteException e)
                {
                        return false;//e.printStackTrace();
                }
        }
        
        //Approve Wiki Contents
        public void approveWiki(int wikiID, int userID)
        {
                try
                {
                        server.approveWiki(wikiID, userID);
                }
                catch(RemoteException e)
                {
                        //e.printStackTrace();
                }               
        }
}